<?php

namespace app\api\controller\v1\plan;

use app\model\course\topic\Topic;
use app\model\exam\Exam;
use app\model\plan\exam\StaffExam;
use app\model\plan\Plan;
use app\model\plan\PlanStaff;
use app\Request;
use think\Exception;
use think\facade\Db;

class ExamController
{

    public function check(Request $request)
    {
        list($planId) = $request->getMore([
            ['plan_id', 0] ,
        ], true);

        $where[] = ['id' ,'=', $planId ] ;
        $where[] = ['staff_id' ,'=', $request->staffId() ] ;
        $planStaff = PlanStaff::where($where)->find() ;
        if($planStaff == []){
            return app('json')->fail('课程不存在') ;
        }
        if($planStaff['exam_ret'] == 9){
            return app('json')->fail('您已考试完成，不可重复考试') ;

        }
        $plan = Plan::getOne($planStaff['plan_id']) ;
        $exam = Exam::getOne($plan['exam_id']) ;

        $count = StaffExam::where('plan_id' , $plan['id'])
            ->where('staff_id' , $request->staffId())
            ->where('exam_progress' , 9)->count() ;
        $join = 0 ;
        $num = $exam['more_num'] - $count  ;
        $msg = '' ;
        if($exam['more_num'] == -1){
            $join = 1 ;
            $msg = '您当前考试不限制次数，交卷计算考试成绩。' ;
        }
        else{
            if($exam['more_num'] > $count){
                $join = 1 ;
                $msg = '您当前考试次数剩余' .$num.'次，交卷计算考试成绩。' ;
            }
            else{
                $msg = '您当前考试次数为0，不可进行考试' ;
            }
        }

        return app('json')->success([
            'join' => $join ,
            'message' => $msg,
        ]);
    }

    public function index(Request $request)
    {
        list($page) = $request->getMore([
            ['page', 1] ,

        ], true);
        $where[] = ['staff_id' , '=' , $request->staffId()] ;
        $where[] = ['exam_progress' , 'in' , [8,9]] ;
        $list = StaffExam::field('point,exam_time,exam_ret,plan_id,exam_id')
            ->where($where)
            ->with(['plan' , 'exam'])->page($page , 10)->order('id desc')->select();

        $list->visible(['plan' => ['plan_name'] , 'exam' => ['exam_name']])->toArray();

        $all = StaffExam::where($where)->count() ;
        $all2 = StaffExam::where($where)->where('exam_ret' , 9)->count() ;
        $all3 = StaffExam::where($where)->where('exam_ret' , 8)->count() ;

        return app('json')->success([
            'stat' => [
                'all' => $all ,
                'pass' => $all2 ,
                'nopass' => $all3 ,
            ] ,
            'list' => $list ,
        ]);
    }
    public function examination(Request $request)
    {
        list($planId) = $request->getMore([
            ['plan_id', 1] ,

        ], true);

        $planStaff = PlanStaff::where('id' , $planId)->find() ;

        $plan  = Plan::getOne($planStaff['plan_id']) ;
        $exam = Exam::where('status' , 1)
            ->field('id,exam_limit_time,dan_num,dan_point,pan_point,duo_point,dan_wareids,duo_wareids,pan_wareids')
            ->where('id' , $plan['exam_id'])->find() ;

        $dan = Topic::examTopic($exam['dan_wareids'] , $exam['dan_point']) ;
        $duo = Topic::examTopic($exam['duo_wareids'], $exam['duo_point']) ;
        $pan = Topic::examTopic($exam['pan_wareids'], $exam['pan_point']) ;
        $result  = array_merge($dan , $duo)  ;
        $result  = array_merge($result , $pan)  ;


        $exam['exam_limit_time'] = $exam['exam_limit_time'] * 60 ;
        $staffExam = StaffExam::create([
            'staff_id' => $request->staffId(),
            'plan_id' => $plan['id'],
            'exam_progress' => 1 ,
            'exam_time' => time() ,
            'company_id' => $request->staffInfo()->company_id ,
            'exam_id' => $plan['exam_id'] ,
        ]) ;

        return app('json')->success([
            'exam' => $exam ,
            'list' => $result ,
            'staff_exam_id' => $staffExam->id ,
        ]);

    }
    public function exam_submit(Request $request)
    {
        list($useTime ,$examId , $examJson , $staffExamId , $point , $allTotal , $total1 , $total2 , $total3) = $request->getMore([
            ['use_time', 0] ,
//            ['plan_id', 1] ,
            ['exam_id', 1] ,
            ['exam_json' , ''] ,
            ['staff_exam_id' , 0] ,
            ['point' , 0] ,
            ['all_total' , 0] ,
            ['total1' , 0] ,
            ['total2' , 0] ,
            ['total3' , 0] ,

        ], true);

        $staffExam = StaffExam::where('id' , $staffExamId)->find() ;

        $exam = Exam::getOne($staffExam['exam_id']) ;

        try {
            Db::startTrans();
            $staffExam->use_time = $useTime ;
            $staffExam->exam_progress = 9 ;
            $staffExam->submit_time = time() ;
            $staffExam->all_total = $allTotal;
            $staffExam->point = $point ;
            $staffExam->total1 = $total1 ;
            $staffExam->total2 = $total2 ;
            $staffExam->total3 = $total3 ;
//            $staffExam->plan_id = $planId ;
            $staffExam->exam_id = $examId ;
            $staffExam->exam_json = json_encode($examJson) ;


            if($point >= $exam['min_point']){
                $staffExam->exam_ret = 9 ;
            }else{
                $staffExam->exam_ret = 8 ;
            }
            $staffExam->save() ;


            $planStaff = [
                'staff_exam_id' => $staffExamId ,
                'exam_point' => $point ,
                'exam_ret' => $staffExam->exam_ret ,
                'exam_time' => time() ,
                'exam_status' => 9 ,
            ] ;
            if($staffExam->exam_ret == 9){
                $planStaff['complete_status'] = 9;
            }

            PlanStaff::where('staff_id' , $staffExam['staff_id'])
                ->where('plan_id' , $staffExam['plan_id'])
                ->update($planStaff) ;


            Db::commit() ;
            return app('json')->success([
                'total1' => $total1 ,
                'total2' => $total2 ,
                'total3' => $total3 ,
                'all_total' => $allTotal ,
                'total' => $total1 ,
                'pass' => $staffExam->exam_ret ,
            ]);
        }
        catch (Exception $e){
            return app('json')->fail($e->getMessage());
        }

    }
}